using System;
using System.Runtime.InteropServices;

namespace InfiniTec.DirectoryServices
{
    internal static class SafeNativeMethods
    {
        public const uint InvalidDomainName = 0x4bc;
        public const uint InvalidParameter = 0x7;
        public const uint NoSuchDomain = 0x54b;
        public const uint OutOfMemory = 0x6;
        public const uint Success = 0x0;


		[DllImport("Ntdsapi.dll", CharSet = CharSet.Unicode)]
		public static extern uint DsBind(
			[MarshalAs(UnmanagedType.LPTStr)] string domainControllerName,
			[MarshalAs(UnmanagedType.LPTStr)]string dnsDomainName,
			out DirectoryServiceConnectionSafeHandle result);


        [DllImport("Ntdsapi.dll")]
        public static extern uint DsBindWithCred(
            string domainControllerName,
            string dnsDomainName,
            AuthenticationSafeHandle authIdentity,
            out DirectoryServiceConnectionSafeHandle result);

        [DllImport("Ntdsapi.dll")]
        public static extern uint DsMakePasswordCredentials(
            string user,
            string domain,
            string password,
            out AuthenticationSafeHandle authIdentity);

        [DllImport("Ntdsapi.dll")]
        public static extern uint DsFreePasswordCredentials(
            IntPtr authIdentity);

        [DllImport("Netapi32.dll")]
        public static extern uint NetApiBufferFree(
            IntPtr buffer);

        [DllImport("Ntdsapi.dll")]
        public static extern uint DsUnBind(
            IntPtr handle);

        [DllImport("Ntdsapi.dll", CharSet = CharSet.Unicode)]
        public static extern uint DsCrackNames(
            DirectoryServiceConnectionSafeHandle handle,
            TranslationFlags translationFlags,
            NameFormat currentNameFormat,
            NameFormat targetNameFormat,
            uint numberOfNames,
            string[] currentNames,
            out IntPtr result);

        [DllImport("Ntdsapi.dll")]
        public static extern uint DsFreeNameResult(IntPtr nameResult);

        [DllImport("Netapi32.dll", CharSet = CharSet.Unicode)]
        public static extern int DsEnumerateDomainTrusts(
        string ServerName,
        TrustFlags Flags,
        out IntPtr Domains,
        out int DomainCount);

        [StructLayout(LayoutKind.Sequential)]
        public class DS_DOMAINTRUST_ENTRY
        {
            public IntPtr NetbiosDomainName;
            public IntPtr DnsDomainName;
            public TrustFlags Flags;
            public uint ParentIndex;
            public uint TrustType;
            public TrustAttributes TrustAttributes;
            public IntPtr DomainSid;
            public Guid DomainGuid;
        }



        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct DS_NAME_RESULT
        {
            public uint Count;
            public IntPtr Items; //  Array of pointers to DS_NAME_RESULT_ITEM
        }

 

        #region Nested type: DS_NAME_RESULT_ITEM

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct DS_NAME_RESULT_ITEM
        {
            public uint Status;
            public string Domain;
            public string Name;
        }

        #endregion
    }
}